package face.pay.util;

import java.math.BigDecimal;

/**
 * @Auther: zkj
 * @Date: 2019/3/12 15:57
 * @Description:
 */
public class MathUtil {

    /**
     * 返向计算比例  doube
     * @param rate
     * @return
     */
    public static Double percentRate(Double rate ){

        BigDecimal a  = new BigDecimal(new Double(100));


        BigDecimal b  = new BigDecimal(rate);


        BigDecimal c  = a.subtract(b);

        Double d =   c.divide(new BigDecimal(100)).doubleValue();

        return d;

    }

    /**
     * 货币转化
     * double 转换成 long
     * 圆转换成分
     * @return
     */
    public static Long transForMoney(Double money){

        Double c =  money*100;

        BigDecimal b  = new BigDecimal(c);
        return  b.longValue();

    }

    public static  Long add( Long a ,Long b){

        if(b==null){
            return  a;
        }else {
            Double c =  new BigDecimal(a.toString()).add(new BigDecimal(b.toString())).doubleValue();

            return  c.longValue();
        }

    }


    public static  Double multiply(Object a ,Object b){

        if(b==null){
            return  Double.valueOf(a.toString());
        }else {
            Double c =  new BigDecimal(a.toString()).multiply(new BigDecimal(b.toString())).doubleValue();

            return  c;
        }

    }

    public static  Double add( Double a ,Double b){

        if(b==null){
            return  a;
        }else {
            Double c =  new BigDecimal(a.toString()).add(new BigDecimal(b.toString())).doubleValue();
            return  c;
        }

    }


    /**
     * long 转换成 double
     * @param money
     * @return
     */
    public static Double transForMoney(Long money){

        BigDecimal a  = new BigDecimal(new Double(100));

        BigDecimal b  = new BigDecimal(money);
        return  b.divide(a).doubleValue();

    }

    /**
     * 保留两位小数
     * @param amount
     * @return
     */
    public static Double transScaleMoney(Long amount) {
        if (amount != null) {
            String money= new BigDecimal(amount).divide(new BigDecimal(100)).setScale(2).toString();
            return  new Double(money);
        }
        return null;
    }



    public static BigDecimal transBigDecimal(Long amount,Double rate) {
        BigDecimal money =   new BigDecimal(amount.toString());

        BigDecimal m = money.multiply( new BigDecimal(rate.toString()));

        BigDecimal c = m.setScale(2,BigDecimal.ROUND_FLOOR);

        return c;
    }


    public static Double sub(Double a,Double b){

        Double c =  new BigDecimal(a.toString()).subtract(new BigDecimal(b.toString())).doubleValue();

        return  c.doubleValue();
    }


    public static Long sub(String a,String b){

        return new BigDecimal(a).subtract(new BigDecimal(b)).longValue();

    }


    public static Long sub(Long a,Long b){

        return new BigDecimal(a.toString()).subtract(new BigDecimal(b.toString())).longValue();

    }

    public static Long profit(Long amount,Double rate) {
        BigDecimal money  = transBigDecimal(Long.valueOf(amount),rate);

        if( money.compareTo(new BigDecimal(1))==-1){

            return  new Long("0");
        }
       return money.setScale( 0, BigDecimal.ROUND_DOWN ).longValue();
    }

    public static void main(String[] args) {

        Double a = new Double("0.0026");

        Double b = new Double("0.0023");

        Double c =  MathUtil.sub(a,b);

        System.out.println( c.compareTo(new Double(0)));





    }


}
